#include "NewSyndrome.h"

int parse_options (int argc, char *argv[])
{
	int i;	
	char *str_user = NULL, *str_group = NULL;
	
	ns_core.ns_log = NULL;
	ns_core.log_default = 1;
	ns_core.nom_conf = NULL;
	ns_core.pidfile = NULL;
	
	if (argc == 1)
		return (1);
	
	for (i = 1; i < argc; i++)
	{
		if (!strcmp (argv[i], "-v") || !strcmp (argv[i], "--version"))
		{
			printf ("%s\n", ns_core.version);
			return (0);
		}
	
		else if (!strcmp (argv[i], "--log") && (i + 1) < argc)
		{
			if (ns_core.ns_log)
				free (ns_core.ns_log);
			ns_core.ns_log = strdup (argv[i + 1]);
			ns_core.log_default = 0;
		}
	
		else if (!strcmp (argv[i], "--conf") && (i + 1) < argc)
		{
			if (ns_core.nom_conf)
				free (ns_core.nom_conf);
			ns_core.nom_conf = malloc (strlen (argv[i + 1]) + 1);
			strcpy (ns_core.nom_conf, argv[i + 1]);
		}
	
		else if (!strcmp (argv[i], "--daemon") || !strcmp (argv[i], "-d"))
		{
			#ifndef WIN32
			daemonize ();
			#endif
		}
		
		else if ((!strcmp (argv[i], "--pidfile") || !strcmp (argv[i], "-p")) && (i + 1) < argc)
		{
			if (ns_core.pidfile)
				free (ns_core.pidfile);
			ns_core.pidfile = strdup (argv[i + 1]);
		}
		
		else if ((!strcmp (argv[i], "--user") || !strcmp (argv[i], "-u")) && (i + 1) < argc)
		{
			str_user = argv[i + 1];
		}
		
		else if ((!strcmp (argv[i], "--group") || !strcmp (argv[i], "-g")) && (i + 1) < argc)
		{
			str_group = argv[i + 1];
		}
		
	}
#ifndef WIN32
	if (str_user || str_group)
		switch_user_group (str_user, str_group);
#endif
	
	if (ns_core.pidfile)
		write_pid_file (ns_core.pidfile);
	
	return (1);
}
